<?php

/**
 * This is the model class for table "{{web_setting_group}}".
 *
 * The followings are the available columns in table '{{web_setting_group}}':
 * @property string $id
 * @property string $name
 * @property string $description
 */
class WebSettingGroup extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return WebSettingGroup the static model class
     */
    public static function model($className=__CLASS__) {return parent::model($className);}

    /**
     * @return string the associated database table name
     */
    public function tableName() {return '{{web_setting_group}}';}

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // Define only rules for those attributes that will receive user inputs.
        return array(
			array('name','required'),
            array('name', 'length', 'max'=>50),
            array('description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, description', 'safe', 'on'=>'search'),
        );
    }

    public function relations()
    {
        return array(
            'settings'=>array(self::HAS_MANY, 'WebSetting','group_id'),
        );
    }
    
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'            => Yii::t('websetting', 'ID'),
            'name'          => Yii::t('websetting', 'Phân nhóm'),
            'description'   => Yii::t('websetting', 'Mô tả'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,false);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('description',$this->description,true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
            'sort'=>array('defaultOrder'=>'name ASC'),
            'pagination'=>array('pageSize'=>Yii::app()->session->get('adminPageSize',25)),
        ));
    }
    
    public function beforeSave()
    {
        if(parent::beforeSave())
        {
            if(trim($this->description)=='') $this->description = "Mô tả";
            return true;
        }
        return false;
    }
    public function beforeDelete()
    {
        if(parent::beforeDelete())
        {
            if($this->id==1) throw new CHttpException(404, Yii::t('websetting','Không được xóa phân nhóm này.'));
            return true;
        }
        return false;
    }

    public function afterDelete()
    {
        parent::afterDelete();
        
        $sql = 'UPDATE {{web_setting}} SET group_id = 1 WHERE group_id = :group_id';
        $cmd = Yii::app()->db->createCommand($sql);
        $cmd->execute(array(':group_id'=>$this->id));
    }
} 